package com.tiger.project.service.impl;

import java.util.Arrays;
import java.util.List;

import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.StringUtils;
import com.tiger.project.domain.*;
import com.tiger.project.domain.req.AssetReceiptAllocations;
import com.tiger.project.mapper.*;
import com.tiger.project.service.IAssetReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【租金收款/租金进账】Service业务层处理
 *
 * @author standard
 * @date 2022-09-27
 */
@Service
public class AssetReceiptServiceImpl implements IAssetReceiptService {
    @Autowired
    private AssetReceiptMapper assetReceiptMapper;
    @Autowired
    private CommonDecisionMapper decisionMapper;
    @Autowired
    private ContractSponsorMapper sponsorMapper;
    @Autowired
    private ContractBillMapper billMapper;
    @Autowired
    private AssetReceiptHistoryMapper historyMapper;
    @Autowired
    private AssetCollectionRecordMapper collectionRecordMapper;
    @Autowired
    private AssetCollectionRecordEnclosureMapper enclosureMapper;
    @Autowired
    private AssetReceiptAllocationMapper allocationMapper;


    /**
     * 查询【租金收款/租金进账】
     *
     * @param id 【租金收款/租金进账】主键
     * @return 【租金收款/租金进账】
     */
    @Override
    public AssetReceipt selectAssetReceiptById(Long id) {
        AssetReceipt assetReceipt = assetReceiptMapper.selectAssetReceiptById(id);
        //决策依据
        String decisionBasisIds = assetReceipt.getDecisionId();
        if (StringUtils.isNotEmpty(decisionBasisIds)){
            List<String> ids = Arrays.asList(decisionBasisIds.split(","));
            for (String i : ids) {
                CommonDecision decision = decisionMapper.selectCommonDecisionById(Long.valueOf(i));
                assetReceipt.getCommonDecisionList().add(decision);
            }
        }
        //合同发起
        ContractSponsor sponsor = new ContractSponsor();
        sponsor.setId(assetReceipt.getContractSponsorId());
        List<ContractSponsor> contractSponsorList = sponsorMapper.selectContractSponsorList(sponsor);
        if (contractSponsorList != null && contractSponsorList.size() > 0){
            assetReceipt.getSponsorList().addAll(contractSponsorList);
        }
        //租金账单
        ContractBill contractBill = new ContractBill();
        contractBill.setContractSponsorId(assetReceipt.getContractSponsorId());
        List<ContractBill> billList = billMapper.selectContractBillList(contractBill);
        if (billList != null && billList.size() > 0){
            assetReceipt.getContractBillList().addAll(billList);
        }
        //附件
        AssetCollectionRecordEnclosure recordEnclosure = new AssetCollectionRecordEnclosure();
        recordEnclosure.setAssetReceiptId(id);
        List<AssetCollectionRecordEnclosure> enclosureList = enclosureMapper.selectAssetCollectionRecordEnclosureList(recordEnclosure);
        if (enclosureList != null &&enclosureList.size() > 0){
            assetReceipt.getEnclosureList().addAll(enclosureList);
        }
        //租金进账收款记录
        AssetCollectionRecord collectionRecord = new AssetCollectionRecord();
        collectionRecord.setAssetReceiptId(id);
        List<AssetCollectionRecord> recordList = collectionRecordMapper.selectAssetCollectionRecordList(collectionRecord);
        if (recordList != null && recordList.size()>0){
            assetReceipt.getCollectionRecordList().addAll(recordList);
        }


        return assetReceipt;
    }

    /**
     * 查询【租金收款】列表
     *
     * @param assetReceipt 【租金收款】
     * @return 【租金收款】
     */
    @Override
    public List<AssetReceipt> selectAssetReceiptList(AssetReceipt assetReceipt) {
        return assetReceiptMapper.selectAssetReceiptList(assetReceipt);
    }


    /**
     *查询租金进账列表
     * @param assetReceipt 租金进账
     * @return
     */
    @Override
    public List<AssetReceipt> selectAssetReceiptRecordList(AssetReceipt assetReceipt) {
        assetReceipt.setIsType("1");
        assetReceipt.setStatus(3L);
        return assetReceiptMapper.selectAssetReceiptRecordList(assetReceipt);
    }


    /**
     * 新增【租金进账】
     *
     * @param assetReceipt 【租金进账】
     * @return 结果
     */
    @Override
    public int addAssetReceiptRecord(AssetReceipt assetReceipt) {
        assetReceipt.setRecordStatus(1L);
        int i = assetReceiptMapper.updateAssetReceipt(assetReceipt);
        change(assetReceipt);
        return i;
    }


    /**
     * 修改【租金收款】
     *
     * @param assetReceipt 【租金收款】
     * @return 结果
     */
    @Override
    public int updateAssetReceipt(AssetReceipt assetReceipt) {
        assetReceipt.setStatus(2L);
        assetReceipt.setUpdatedAt(DateUtils.getNowDate());
        return assetReceiptMapper.updateAssetReceipt(assetReceipt);
    }


    /**
     * 修改【租金进账】
     *
     * @param assetReceipt 【租金进账】
     * @return 结果
     */
    @Override
    public int updateAssetReceiptRecord(AssetReceipt assetReceipt) {
        assetReceipt.setUpdatedAt(DateUtils.getNowDate());
        change(assetReceipt);
        return assetReceiptMapper.updateAssetReceipt(assetReceipt);
    }


    /**
     * 开票申请审核回调
     * @param id
     * @param status 审核状态
     * @param oaFile OA返回文件链接
     * @param isMakeInvoice isMakeInvoice
     * @param isGetInvoice isGetInvoice
     * @return
     */
    @Override
    public int callBack(Long id,Long status,String oaFile,String isMakeInvoice,String isGetInvoice) {
        //开票申请审核通过之后，租金到账，租金进账同时有数据
        ContractBill bill = new ContractBill();
        bill.setId(id);
        bill.setStatus(status);
        bill.setOaFile(oaFile);
        bill.setIsMakeInvoice(isMakeInvoice);
        bill.setIsGetInvoice(isGetInvoice);
        return billMapper.updateContractBill(bill);
    }

    /**
     * 租金进账审核回调
     * @param
     * @return
     */
    @Override
    public int callBackRecord(Long id,Long status,String recordOaFile) {
        AssetReceipt assetReceipt = assetReceiptMapper.selectAssetReceiptById(id);
        AssetReceiptHistory history = new AssetReceiptHistory();
        alterHistory(history,assetReceipt);
        history.setReceiptNum(assetReceipt.getReceiptNum());
        history.setIsType("1");
        historyMapper.insertAssetReceiptHistory(history);
        assetReceipt.setUpdatedAt(DateUtils.getNowDate());
        assetReceipt.setRecordStatus(status);
        assetReceipt.setRecordOaFile(recordOaFile);
        return assetReceiptMapper.updateAssetReceipt(assetReceipt);
    }


    //收款记录列表
    @Override
    public List<AssetReceiptHistory> selectAssetReceiptRecordsList(Long id,String type) {
        AssetReceiptHistory history = new AssetReceiptHistory();
        history.setAssetReceiptId(id);
        history.setIsType(type);
        return historyMapper.selectAssetReceiptHistoryList(history);
    }

    @Override
    public AssetReceiptHistory selectAssetReceiptRecordsById(Long id) {
        AssetReceiptHistory history = historyMapper.selectAssetReceiptHistoryById(id);
        //决策依据
        String decisionBasisIds = history.getDecisionId();
        if (decisionBasisIds != null){
            List<String> ids = Arrays.asList(decisionBasisIds.split(","));
            for (String i : ids) {
                CommonDecision decision = decisionMapper.selectCommonDecisionById(Long.valueOf(i));
                history.getCommonDecisionList().add(decision);
            }
        }
        return history;
    }

    /**
     * 批量删除【租金进账】
     *
     * @param id 需要删除的【租金进账】主键
     * @return 结果
     */
    @Override
    public int deleteAssetReceiptById(Long id) {
        AssetReceipt assetReceipt = assetReceiptMapper.selectAssetReceiptById(id);
        enclosureMapper.deleteAssetCollectionRecordEnclosureByReceiptId(id);
        collectionRecordMapper.deleteAssetCollectionRecordById(assetReceipt.getId());
        assetReceipt.setRecordStatus(0L);
        return assetReceiptMapper.updateAssetReceipt(assetReceipt);
    }



    private void change(AssetReceipt assetReceipt){
        //附件
        enclosureMapper.deleteAssetCollectionRecordEnclosureByReceiptId(assetReceipt.getId());
        if (StringUtils.isNotEmpty(assetReceipt.getEnclosureList())){
            assetReceipt.getEnclosureList().forEach(item ->{
                item.setAssetReceiptId(assetReceipt.getId());
                item.setCreatedAt(DateUtils.getNowDate());
                item.setState("1");
                enclosureMapper.insertAssetCollectionRecordEnclosure(item);
            });
        }

        //租金进账记录
        collectionRecordMapper.deleteAssetCollectionRecordById(assetReceipt.getId());
        if (StringUtils.isNotEmpty(assetReceipt.getCollectionRecordList())){
            assetReceipt.getCollectionRecordList().forEach(record ->{
                record.setAssetReceiptId(assetReceipt.getId());
                collectionRecordMapper.insertAssetCollectionRecord(record);
            });
        }

    }


    private void alterHistory(AssetReceiptHistory history,AssetReceipt assetReceipt){
        history.setAssetReceiptId(assetReceipt.getId());
        history.setContractSponsorNo(assetReceipt.getContractSponsorNo());
        history.setContractSponsorId(assetReceipt.getContractSponsorId());
        history.setAssetId(assetReceipt.getAssetId());
        history.setAssetName(assetReceipt.getAssetName());
        history.setDecisionId(assetReceipt.getDecisionId());
        history.setLesseeId(assetReceipt.getLesseeId());
        history.setLesseeName(assetReceipt.getLesseeName());
        history.setLessorId(assetReceipt.getLessorId());
        history.setLessorName(assetReceipt.getLessorName());
        history.setInvoiceAmount(assetReceipt.getInvoiceAmount());
        history.setCreditAmount(assetReceipt.getCreditAmount());
        history.setIsDedicated(assetReceipt.getIsDedicated());
        history.setRemark(assetReceipt.getRemark());
        history.setRentRental(assetReceipt.getRentRental());
    }


}
